﻿using System;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.ProtoBase;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperSocketV2Sample.Config
{
    class Program
    {
        static async Task Main(string[] args)
        {
            //创建宿主：用Package的类型和PipelineFilter的类型创建SuperSocket宿主。
            var host = SuperSocketHostBuilder.Create<StringPackageInfo, CommandLinePipelineFilter>()
                //注册用于处理连接、关闭的Session处理器
                .UseSessionHandler(async (session) =>
                {
                    Console.WriteLine($"session connected: {session.RemoteEndPoint}");

                    //发送消息给客户端
                    var msg = $@"Welcome to TelnetServer: {session.RemoteEndPoint}";
                    await session.SendAsync(Encoding.UTF8.GetBytes(msg + "\r\n"));
                }, async (session, reason) =>
                {
                    await Task.Delay(0);
                    Console.WriteLine($"session {session.RemoteEndPoint} closed: {reason}");
                })
                //注册用于处理接收到的数据的包处理器
                .UsePackageHandler(async (session, package) =>
                {
                    Console.WriteLine($"session ip: {session.RemoteEndPoint}, " +
                                      $" command: {package.Key}");

                    string result;
                    try
                    {
                        switch (package.Key.ToUpper())
                        {
                            case "ECHO":
                                result = package.Body;
                                break;

                            case ("ADD"):
                                result = package.Parameters
                                    .Select(t => int.Parse(t))
                                    .Sum()
                                    .ToString();
                                break;

                            case ("SUB"):
                                result = package.Parameters
                                    .Select(t => int.Parse(t))
                                    .Aggregate((x, y) => x - y)
                                    .ToString();
                                break;

                            case ("MULT"):
                                result = package.Parameters
                                    .Select(t => int.Parse(t))
                                    .Aggregate((x, y) => x * y)
                                    .ToString();
                                break;

                            default:
                                result = "Unknown command";
                                break;
                        }
                    }
                    catch (Exception e)
                    {
                        result = e.Message;
                    }

                    //发送消息给客户端
                    await session.SendAsync(Encoding.UTF8.GetBytes(result + "\r\n"));
                })
                //配置日志
                .ConfigureLogging((hostCtx, loggingBuilder) =>
                {
                    loggingBuilder.AddConsole();
                })
                .Build();
            await host.RunAsync();
        }
    }
}
